/**
 * 弹出window
 */
base = base || {};

base.layer_index = 100;

base.showWindow = function(opt) {

	var option = {
		title : '窗口',
		$el : null,
		btnAndHandlers : null,
		width : 600,
		height : 400,
		closeHandler : null,
		bottomHeight : 50,
		$container : null
	};

	// option.btnAndHandlers = option.btnAndHandlers || [ {
	// text : '取消',
	// handler : function() {
	//			
	// }
	// }, {
	// text : '确定',
	// handler : function() {
	// if (defineHandler) {
	// defineHandler.call(this);
	// }
	// }
	// } ];

	$.extend(option, opt);
	var dom = '';
	dom += '<div class="common-window mask-layer" >';
	dom += '	<div class="mask-layer-txt">';
	dom += '		<div class="mask-layer-txt-main">';
	dom += '			<div class="mask-layer-txt-main-title">';
	dom += '				<i class="tl"></i>';
	dom += '				<div class="number-close">';
	dom += '					<a class="close">×</a>';
	dom += '				</div>';
	dom += '			</div>';
	dom += '			<div class="mask-layer-txt-main-content">';
	dom += '			</div>';
	dom += '			<div class="mask-layer-txt-main-bottom">';
	dom += '			</div>';
	dom += '		</div>';
	dom += '	</div>';
	dom += '</div>';

	var $dom = $(dom).hide().css({'zIndex':(++base.layer_index)});
	
	if (option.$container != null&&option.$container.length>0) {
		option.$container.append($dom);
	} else {
		window.top.$('body').append($dom);
	}
	

	$win = $dom.find('.mask-layer-txt');

	$win.width(option.width);
	$win.height(option.height);

	$win.css({
		'marginLeft' : '-' + option.width / 2 + 'px',
		'marginTop' : '-' + option.height / 2 + 'px'
	});

	$content = $dom.find('.mask-layer-txt-main-content').append(option.$el.show());
	if(option.title==null||option.title.trim()==''){
		$dom.find('.mask-layer-txt-main-title').css({
			'border':'none'
		});
	}
	$dom.find('.mask-layer-txt-main-title .tl').append(option.title);

	$bottom = $dom.find('.mask-layer-txt-main-bottom');


	if (!option.btnAndHandlers || option.btnAndHandlers.length == 0) {
		$bottom.hide();
		$content.css('bottom', 0);
	} else {
		
		$(option.btnAndHandlers).each(
				function(i, btn) {
					$bottom.append('<a class="mask-layer-txt-main-btn"  xindex="'
							+ i + '">' + btn.text + '</a> ');
				});
		
		$dom.find('.mask-layer-txt-main-btn').bind('click', function() {
			var index = $(this).attr('xindex');
			var res = option.btnAndHandlers[index].handler.call($dom);
			if (res) {
				hide();
			}
		});
		$content.css('bottom', option.bottomHeight+'px');
		$bottom.css('height', option.bottomHeight+'px');
	}
	
	$dom.find('.close').bind('click', function() {
		hide();
	});
	function hide() {
		$dom.hide();
		if (option.closeHandler) {
			option.closeHandler.call($dom);
		}
	}
	
	
	$dom.distoryWin = function(){
		$dom.find('.close').unbind('click');
		$dom.find('.mask-layer-txt-main-btn').unbind('click');
		$dom.remove();
	}
	
	

	return {
		_this : $dom,
		hide : function() {
			hide();
		},
		show : function() {
			$dom.show();
		},
		content : option.$el
	}
}

base.showWindowWithOutBtn = function(opt) {

}

base.showConfirm = function(msg, handler, title,className) {
	var $el = $('<div>').css({
		'fontSize': '20px',
		'padding': '8px 13px',
		'wordWrap': 'break-word',
		'wordBreak': 'break-all',
		'textAlign' : 'center',
		'color' : '#333',
	    'marginTop': '28px',
	    'fontSize' : '18px',
	    'fontFamily' : 'Arial, 微软雅黑, Helvetica, sans-serif',
	    'z-index':'9999'
	}).html(msg);
	
	var option = {
			title : title,
			$el : $el,
			btnAndHandlers : null,
			width : 530,
			height : 280,
			closeHandler : null,
			bottomHeight : 80
		};
	option.$container = className==null?null:$('.'+className);
	
	option.btnAndHandlers =  [ {
		text : '确定',
		handler : function() {
			if (handler) {
				handler.call(this);
			}
			return true;
		},
		css:'padding-top:-100px'
	} ];
	
	option.closeHandler = function (){
		this.distoryWin();
	}
	
	base.showWindow(option,className).show();
}

base.showMsg = function(msg, handler, title,className) {
	title = title || "提示信息";
	return base.showConfirm(msg, handler, title,className);
}